package com.tiantian.quanminxiu.presenter;

import android.util.Log;

import com.tiantian.quanminxiu.api.SocketUtils;
import com.tiantian.quanminxiu.event.SocketStateEvent;
import com.tiantian.quanminxiu.utils.EventBusUtils;
import com.tiantian.quanminxiu.view.DialogUserlistView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by admin on 2016/9/14.
 */
public class DialogUserListPresent implements Presenter<DialogUserlistView> {

    private DialogUserlistView dialogUserlistView;

    @Override
    public void attachView(DialogUserlistView view) {
        this.dialogUserlistView = view;
        EventBusUtils.getInstance().register(this);
    }

    @Override
    public void detachView() {
        this.dialogUserlistView = null;
        EventBusUtils.getInstance().unregister(this);
    }

    public void getUserList(){
        dialogUserlistView.Loading_Open();
        sendLiveMessage("UserList");//请求用户列表
    }

    public void getAdminList(){
        dialogUserlistView.Loading_Open();
        sendLiveMessage("AdminList");//管理员列表
    }
    public void getBlackList(){
        dialogUserlistView.Loading_Open();
        sendLiveMessage("BlackList");//黑名单列表
    }

    //region 接受socket状态消息 receiveStateSocket(SocketStateEvent event)
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveStateSocket(SocketStateEvent event){
        dialogUserlistView.Loading_Close();
        if(event.getJsonObject()==null){
            return;
        }
        Log.i("receiveMessage", "--receiveMessage:" + event.getJsonObject().toString());
        JSONObject jsonObject = event.getJsonObject();
        String Type= jsonObject.optString("Type");
        switch (event.getSocketTyep()) {
            case SocketUtils.CONNECTERROR:
            case SocketUtils.DISCONNECT:
                //连接socket服务器出错
                break;
            case SocketUtils.CONNECT:
                //连接到socket服务器
                break;
            case SocketUtils.USERLISTLINSENER:
                //region 用户列表(未完成) USERLISTLINSENER
                JSONArray arr = jsonObject.optJSONArray("Data");
                dialogUserlistView.getUserList(arr);
                //当前房间数人数 （未完成）
                //endregion
                break;
            case SocketUtils.ADMINLISTLINSENER:
                //region 用户列表(未完成) USERLISTLINSENER
                JSONArray arrAdmin = jsonObject.optJSONArray("Data");
                dialogUserlistView.getAdminList(arrAdmin);
                //当前房间数人数 （未完成）
                //endregion
                break;
            case SocketUtils.BLACKLISTLINSENER:
                //region 用户列表(未完成) USERLISTLINSENER
                JSONArray arrBlack = jsonObject.optJSONArray("Data");
                dialogUserlistView.getBlackList(arrBlack);
                //当前房间数人数 （未完成）
                //endregion
                break;

        }
    }

    //region 发送socket消息 sendLiveMessage(String command,Object... args)
    public void sendLiveMessage(String command,Object... args) {
        SocketUtils.getInstance().sendMessage(command, args);
    }
    //endregion
}
